package cn.tf.tfpay.fap.server;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.apache.commons.codec.binary.Base64;

import cn.tf.tfpay.fap.server.RemoteFile.FileType;

public abstract class FapUtils {
	
	public static String encodePath(String path) {
		return Base64.encodeBase64URLSafeString(path.trim().getBytes(StandardCharsets.UTF_8));
	}

	public static String decodePath(String base64Path) {
		return new String(Base64.decodeBase64(base64Path), StandardCharsets.UTF_8).trim();
	}
	
	public static RemoteFile buildRemoteFile(File localFile) {
		try {
			return new RemoteFile(localFile.getCanonicalPath()).setName(localFile.getName())
					.setType(localFile.isDirectory() ? FileType.DIRECTORY : FileType.FILE)
					.setLength(localFile.isDirectory() ? -1 : localFile.length()).setReadable(localFile.canRead())
					.setWriteable(localFile.canWrite());
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage());
		}
	}
	
}
